package web.beans;

import ejb.entities.Event;
import ejb.entities.Invitation;
import ejb.facade.EventFacade;
import ejb.facade.InvitationFacade;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

@ViewScoped
@Named("notificationsBean")
public class NotificationsCenter implements Serializable{
    
        private static final long serialVersionUID = 1L;
        
        @EJB
	private InvitationFacade inFacade;
        @EJB
	private EventFacade evFacade;
        
        @Inject
	private Login login;
        
        private Invitation selectedInvite;
        private List<Invitation> invitesList;
        
        private Event selectedEvent;
        private List<Event> badeventList;
        
        @PostConstruct
        public void init(){
            this.invitesList=inFacade.findInvitationByUserAfterToday(login.getUser());
            this.badeventList=evFacade.findEventWithBadWeather();
        }
        
   //SETTER Functions
        
    public void setEvFacade(EventFacade evFacade) {
        this.evFacade = evFacade;
    }

    public void setSelectedEvent(Event selectedEvent) {
        this.selectedEvent = selectedEvent;
    }

    public void setBadeventList(List<Event> badeventList) {        
        this.badeventList = badeventList;
    }

    public void setSelectedInvite(Invitation selectedInvite) {
        this.selectedInvite = selectedInvite;
    }

    public void setLogin(Login login) {
        this.login = login;
    }
    
    public void setInFacade(InvitationFacade inFacade) {
        this.inFacade = inFacade;        
    }      
    
    public void setInvitesList(List<Invitation> invitesList) {
        this.invitesList = invitesList;
    }            
    
    //GETTER Functions
    
    public EventFacade getEvFacade() {
        return evFacade;
    }

    public Event getSelectedEvent() {
        return selectedEvent;
    }


    public List<Event> getBadeventList() {    
        return badeventList;
    }

    public Invitation getSelectedInvite() {
        return selectedInvite;
    }

    public Login getLogin() {
        return login;
    }
    
    public InvitationFacade getInFacade() {
        return inFacade;
    }
    
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public List<Invitation> getInvitesList() {
        return invitesList;
    }

        //BEAN Fuctions
    
    public void acceptInvite(Invitation inv){
        inv.setInvstate("A");
        inFacade.update(inv);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Accepted!", "Invitation Accepted!"));  
    }
    
    public void declineInvite(Invitation inv){
        inv.setInvstate("D");
        inFacade.update(inv);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Declined!", "Invitation Declined!")); 
    }
    
      public void goToEditEvent(Event e){
        redirect("user_addNewEvent.xhtml", e);
    }
      
      public void redirect(String outcome, Event e) {
        login.setEventSelected(e.getIdEvent());
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.getApplication().getNavigationHandler().handleNavigation(facesContext, null, outcome);
    }
    
}
